package www.way2inbox.data;

import android.app.Activity;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;

import java.util.ArrayList;
import java.util.List;

import www.way2inbox.common.PrintMsg;

/**
 * Created by sys1031 on 12/12/14.
 */
public class SmsDatabase  {

    public String _uri_inbox = "content://sms/inbox";
    public String _uri_sent = "content://sms/sent";
    int i = 1;
    public String _id = "_id";
    public String _address = "address";
    public String _person = "person";
    public String _body = "body";
    public String _read = "read";
    public String _date = "date";
    public String _type = "type";
    public String _inbox = "inbox";
    public String _sent = "sent";
    public String _seen = "seen";

    Context context;
    Cursor Message_distinct, Message_single, finalCC,finalcc1;
    Native_DatabaseHelper helper;
    Uri uri,uri1;

    public SmsDatabase(Context context) {
        this.context = context;
    }

    public enum GetType {
        Inbox("1"), Sent("0"), Read("1"), Seen("1");

        private String statusCode;

        private GetType(String s) {
            statusCode = s;
        }

        public String getCode() {
            return statusCode;
        }

    }
//
//    public List<smsfetch> getAllSms(Activity mActivity, String Type,
//                                    String SingleYesNo, String Number) {
//        PrintMsg.setprint(mActivity, "got Details as :" + Type + "---"
//                + SingleYesNo + "---" + Number);
//        List<smsfetch> lstSms = new ArrayList<smsfetch>();
//        smsfetch objSms = new smsfetch();
//        uri = Uri.parse(_uri_inbox);
//        uri1=Uri.parse(_uri_sent);
//
//        ContentResolver cr = mActivity.getContentResolver();
//        if (SingleYesNo != null) {
//            helper = new Native_DatabaseHelper(mActivity);
//            PrintMsg.setprint(mActivity, "SingleYesNo is not null");
//            if (SingleYesNo.equalsIgnoreCase("All")) {
//                PrintMsg.setprint(mActivity, "Step for All sms");
//
//                finalCC = cr.query(uri, new String[] { "address," + _id + ","
//                        + _person + "," + _body + "," + _date + "," + _read
//                        + "," + _seen + "," + _type }, null, null, null);
//                finalcc1=cr.query(uri1, new String[]{_address+","+_id+","+_body+","+_date+","+_type}, null, null, null);
//                // finalCC = Message_distinct;
//            }
//            if (SingleYesNo.equalsIgnoreCase("Single")) {
//                PrintMsg.setprint(mActivity, "Step for Single sms");
//                if (Number != null) {
//                    finalCC = cr.query(uri, null, "address='" + Number + "'",
//                            null, "date desc");
//                    // finalCC = Message_single;
//                }
//
//            }
//        }
//
//        PrintMsg.setprint(mActivity, "Cursor Intialized");
//
//        // mActivity.startManagingCursor(finalCC);
//        if (finalCC != null) {
//            PrintMsg.setprint(mActivity, "Cursor is not null");
//            int totalSMS = finalCC.getCount();
//
//            int ii = 0;
//            System.out.println("this is raw sms total" + totalSMS);
//            PrintMsg.setprint(mActivity, "Cursor :::" + "-----" + totalSMS);
//            while (finalCC.moveToNext()) {
//                // for (int i = 0; i < totalSMS; i++) {
//                // PrintMsg.setprint(mActivity,"Cursor "+finalCC.getColumnName(ii));
//                objSms = new smsfetch();
//                String id, address, name, body, read, date, seen, type;
//                id = finalCC.getString(finalCC.getColumnIndexOrThrow(_id));
//                address = finalCC.getString(finalCC
//                        .getColumnIndexOrThrow(_address));
//                name = getContactName(mActivity, finalCC.getString(finalCC
//                        .getColumnIndexOrThrow(_address)));
//                body = finalCC.getString(finalCC.getColumnIndexOrThrow(_body));
//                read = finalCC.getString(finalCC.getColumnIndex(_read));
//                date = finalCC.getString(finalCC.getColumnIndexOrThrow(_date));
//                seen = finalCC.getString(finalCC.getColumnIndexOrThrow(_seen));
//                objSms.setId(id);
//                objSms.setAddress(address);
//                objSms.set_person(name);
//                objSms.setMsg(body);
//                objSms.setReadState(read);
//                objSms.setTime(date);
//                objSms.set_seen(seen);
//                if (finalCC.getString(finalCC.getColumnIndexOrThrow(_type))
//                        .contains("1")) {
//                    type = _inbox;
//
//                } else {
//                    type = _sent;
//                }
//                objSms.setFolderName(type);
//                helper.insertMessage(id, address, body, seen, read, type, date,"SIM");
//
//                System.out.println("inserted " + i + body);
//                i++;
//
//                lstSms.add(objSms);
//
//                // ii++;
//                // }
//            }
//            PrintMsg.setprint(context, "" + lstSms.size());
//            // else {
//            // throw new RuntimeException("You have no SMS");
//            // }
//
//            finalCC.close();
//        }
//        if (finalcc1 != null) {
//            PrintMsg.setprint(mActivity, "Cursor is not null");
//            int totalSMS = finalcc1.getCount();
//
//            int ii = 0;
//            System.out.println("this is raw sms total" + totalSMS);
//            PrintMsg.setprint(mActivity, "Cursor :::" + "-----" + totalSMS);
//            while (finalcc1.moveToNext()) {
//                // for (int i = 0; i < totalSMS; i++) {
//                // PrintMsg.setprint(mActivity,"Cursor "+finalCC.getColumnName(ii));
//                objSms = new smsfetch();
//                String id, address, name, body, read, date, seen, type;
//                id = finalcc1.getString(finalcc1.getColumnIndexOrThrow(_id));
//                address = finalcc1.getString(finalcc1
//                        .getColumnIndexOrThrow(_address));
////				name = getContactName(mActivity, finalCC.getString(finalCC
////						.getColumnIndexOrThrow(_address)));
//                body = finalcc1.getString(finalcc1.getColumnIndexOrThrow(_body));
//                read = "1";
//                date = finalcc1.getString(finalcc1.getColumnIndexOrThrow(_date));
//                seen = "1";
//                objSms.setId(id);
//                objSms.setAddress(address);
////				objSms.set_person(name);
//                objSms.setMsg(body);
//                objSms.setReadState(read);
//                objSms.setTime(date);
//                objSms.set_seen(seen);
//                type = _sent;
//                objSms.setFolderName(type);
//                helper.insertMessage(id, address, body, seen, read, type, date,"SIM");
//
//                System.out.println("inserted " + i + body);
//                i++;
//
//                lstSms.add(objSms);
//
//                // ii++;
//                // }
//            }
//            PrintMsg.setprint(context, "" + lstSms.size());
//            // else {
//            // throw new RuntimeException("You have no SMS");
//            // }
//
//            finalcc1.close();
//        }
//        PrintMsg.setprint(mActivity, "Data is set and returned back");
//
//        return lstSms;
//    }

    public static String getContactName(Context context, String phoneNumber) {
        ContentResolver cr = context.getContentResolver();
        Uri uri = Uri.withAppendedPath(
                ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
                Uri.encode(phoneNumber));
        Cursor cursor = cr.query(uri,
                new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME },
                null, null, null);
        if (cursor == null) {
            return null;
        }
        String contactName = null;
        if (cursor.moveToFirst()) {
            contactName = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
        } else {
            contactName = phoneNumber;
        }

        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }

        return contactName;
    }

    Intent Result=null;
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";
    public Intent SendingMMS(String SMSrMMS,String EnvironmentPathFile,String ToAddress,String WithBody,String TypeMMS){

        Uri uri = Uri.parse("file://"+EnvironmentPathFile);

        Intent res = new Intent(Intent.ACTION_SEND);
        res.putExtra("address",ToAddress);
        res.putExtra("sms_body",WithBody);
        res.putExtra(Intent.EXTRA_STREAM,"file:/"+uri);

        if(TypeMMS!=null && TypeMMS.equalsIgnoreCase("image")) {
            res.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
            res.setType("image/*");

        }
        if(TypeMMS!=null && TypeMMS.equalsIgnoreCase("audio")) {
            res.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
            res.setType("audio/*");
        }
        if(TypeMMS!=null && TypeMMS.equalsIgnoreCase("video")) {
            res.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
            res.setType("video/*");
        }
        if(TypeMMS!=null && TypeMMS.equalsIgnoreCase("mixed")) {
            res.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
            res.setType("image/png");
        }




    return Result=res;
    }

}
